//app.js
App({
  onLaunch: function () {

    wx.getSetting({
      success: res => {

        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => { 

              // 可以将 res 发送给后台解码出 unionId
              this.setUserinfo();

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    });

  },
  

  setUserinfo: function (){
      var _this=this; 
      //先判断缓存中有没有
      
      if(this.globalData.userInfo==null){
        wx.showLoading({
          title: '加载中',
          mask:true
        })
         wx.login({
           success:function (res){
             if (res.code) {
               wx.getUserInfo({
                 withCredentials: true,
                 success:function(res_user){
                   wx.request({
                     url: _this.globalData.apiurl + 'api/index/add_user',
                     method: 'GET',
                     data: {
                       code: res.code,
                       encryptedData: res_user.encryptedData,
                       iv: res_user.iv
                     },
                     header: {
                       'content-type': 'json'
                     },
                     success: function (datas) {
                       _this.globalData.userInfo = datas.data;
                       //console.log(_this.globalData.userInfo)
                       setTimeout(function () {
                         wx.hideLoading()
                       }, 1000)
                     }
                   });

                 }
               })
              }
           }
         })
       
      }

  },
 
  globalData: {
    userInfo: null,
    apiurl:"https://fastadmin.ranmengwork.cn/",
    formType:"",
    id: {},
    change: '',
    name: '',
    sex: '',
    idcard: '',
    phone: '',
    changeform: [{ Dname: "" }, { Didcard: "" }, { Dphone: "" }, { name: "" }, { idcard: "" }, { phone: "" }],
  },
  


})